﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Zen4SyncDashboard.DAL.Referential;

namespace Zen4SyncDashboard.DAL
{
    public static class Zen4SyncModel
    {
        private static Zen4SyncRepositoryEntities Entities = new Zen4SyncRepositoryEntities();

        public static class Internet
        {
            public static internet Create()
            {
                internet internet = new internet();
                internet.internet_id = Guid.NewGuid();
                return internet;
            }
            
            public static ObservableCollection<internet> GetAll()
            {
                return new ObservableCollection<internet>(Zen4SyncModel.Entities.internets);
            }

            public static void Add(internet internet)
            {
                Zen4SyncModel.Entities.internets.AddObject(internet);
                Zen4SyncModel.Entities.SaveChanges();
            }

            public static void Delete(internet internet)
            {
                Zen4SyncModel.Entities.internets.DeleteObject(internet);
                Zen4SyncModel.Entities.SaveChanges();
            }
        }

        public static class Publisher
        {
            public static ObservableCollection<publisher> GetAll()
            {
                return new ObservableCollection<publisher>(
                    Zen4SyncModel.Entities
                        .publishers
                        .Include("securityType")
                        .Include("networkType"));
            }

            public static void Add(publisher publisher)
            {
                Zen4SyncModel.Entities.publishers.AddObject(publisher);
                Zen4SyncModel.Entities.SaveChanges();
            }

            public static void Delete(publisher publisher)
            {
                Zen4SyncModel.Entities.publishers.DeleteObject(publisher);
                Zen4SyncModel.Entities.SaveChanges();
            }

            public static publisher Create()
            {
                publisher publisher = new publisher();
                publisher.publisher_id = Guid.NewGuid();
                return publisher;
            }
        }

        public static class Distributor
        {
            public static ObservableCollection<distributor> GetAll()
            {
                return new ObservableCollection<distributor>(
                    Zen4SyncModel.Entities
                        .distributors
                        .Include("securityType")
                        .Include("networkType"));
            }

            public static void Add(distributor distributor)
            {
                Zen4SyncModel.Entities.distributors.AddObject(distributor);
                Zen4SyncModel.Entities.SaveChanges();
            }

            public static void Delete(distributor distributor)
            {
                Zen4SyncModel.Entities.distributors.DeleteObject(distributor);
                Zen4SyncModel.Entities.SaveChanges();
            }

            public static distributor Create()
            {
                distributor distributor = new distributor();
                distributor.distributor_id = Guid.NewGuid();
                return distributor;
            }
        }

        public static class NetworkType
        {
            public static networkType[] GetAll()
            {
                return Zen4SyncModel.Entities.networkTypes.ToArray();
            }
        }

        public static class SecurityType
        {
            public static securityType[] GetAll()
            {
                return Zen4SyncModel.Entities.securityTypes.ToArray();
            }
        }

        public static class SnapshotTransferType
        {
            public static snapshotTransferType[] GetAll()
            {
                return Zen4SyncModel.Entities.snapshotTransferTypes.ToArray();
            }
        }

        public static class ExchangeType
        {
            public static exchangeType[] GetAll()
            {
                return Zen4SyncModel.Entities.exchangeTypes.ToArray();
            }
        }

        public static class ValidateType
        {
            public static ObservableCollection<validateType> GetAll()
            {
                return new ObservableCollection<validateType>(Zen4SyncModel.Entities.validateTypes);
            }
        }

        public static class SyncSession
        {
            public static syncSession Create()
            {
                syncSession syncSession = new syncSession();
                syncSession.syncSession_id = Guid.NewGuid();
                // Applying default values
                syncSession.syncSession_connectionRetryTimeout = 120;
                syncSession.syncSession_connectTimeout = 15;
                syncSession.syncSession_queryTimeout = 300;
                syncSession.syncSession_receiveTimeout = 60;

                return syncSession;
            }
            public static ObservableCollection<syncSession> GetAll()
            {
                return new ObservableCollection<syncSession>(Zen4SyncModel.Entities.syncSessions);
            }

            public static void Add(syncSession syncSession)
            {
                Zen4SyncModel.Entities.syncSessions.AddObject(syncSession);
                Zen4SyncModel.Entities.SaveChanges();
            }
        }

        public static class SyncSessionContext
        {
            public static syncSessionContext Create()
            {
                syncSessionContext context = new syncSessionContext();
                context.syncSessionContext_id = Guid.NewGuid();
                return context;
            }

            public static ObservableCollection<syncSessionContext> GetAll()
            {
                return new ObservableCollection<syncSessionContext>(
                    Zen4SyncModel.Entities
                    .syncSessionContexts
                    .Include("subscriber.validateType"));
            }

            public static void Add(syncSessionContext context)
            {
                Zen4SyncModel.Entities.syncSessionContexts.AddObject(context);
                Zen4SyncModel.Entities.SaveChanges();
            }
        }

        public static class Subscriber
        {
            public static subscriber Create()
            {
                subscriber sub = new subscriber();
                sub.subscriber_id = Guid.NewGuid();
                // Apply defaut values.
                sub.subscriber_connectionString = "Data source='{0}'";

                return sub;
            }

            public static ObservableCollection<subscriber> GetAll()
            {
                return new ObservableCollection<subscriber>(Zen4SyncModel.Entities.subscribers);
            }
        }

        public static class Activity
        {
            public static activity Create()
            {
                activity act = new activity();
                act.activity_id = Guid.NewGuid();
                return act;
            }
        }

        public static class TestScenario
        {
            public static testScenario Create()
            {
                testScenario scenario = new testScenario();
                scenario.testScenario_id = Guid.NewGuid();

                return scenario;
            }

            public static ObservableCollection<testScenario> GetAll()
            {
                return new ObservableCollection<testScenario>(
                    Zen4SyncModel.Entities
                    .testScenarios
                    .Include("activities"));
            }

            public static void Add(testScenario scenario)
            {
                Zen4SyncModel.Entities.testScenarios.AddObject(scenario);
                Zen4SyncModel.Entities.SaveChanges();
            }
        }

        public static class ActivityType
        {
            public static ObservableCollection<activityType> GetAll()
            {
                var activityTypes = Zen4SyncModel.Entities.activityTypes;

                foreach (var activityType in activityTypes)
                {
                    Guid typeId = activityType.activityType_id;
                    IQueryable<Guid> availableIds = null;

                    if (typeId == GuidReferential.ActivityType.MefExecution)
                        availableIds = null;
                    else if (typeId == GuidReferential.ActivityType.SqlCEBackup)
                        availableIds = null;
                    else if (typeId == GuidReferential.ActivityType.SqlCECheckData)
                        availableIds = null;
                    else if (typeId == GuidReferential.ActivityType.SqlCEExecution)
                        availableIds = null;
                    else if (typeId == GuidReferential.ActivityType.SqlServerCheckData)
                        availableIds = null;
                    else if (typeId == GuidReferential.ActivityType.SqlServerExecution)
                        availableIds = null;
                    else if (typeId == GuidReferential.ActivityType.SyncSession)
                        availableIds = Zen4SyncModel.Entities.syncSessions.Select(session => session.syncSession_id);


                    activityType.SetAvailableActivities(availableIds);
                }

                return new ObservableCollection<activityType>(activityTypes);
            }
        }

        public static class TestSession
        {
            public static testSession Create()
            {
                testSession session = new testSession();
                session.testSession_id = Guid.NewGuid();
                session.testSession_testSessionStateId = GuidReferential.TestSessionState.Planned;

                return session;
            }

            public static void Add(testSession session)
            {
                session.testSession_createdOn = DateTime.UtcNow;

                Zen4SyncModel.Entities.testSessions.AddObject(session);
                Zen4SyncModel.Entities.SaveChanges();
            }

            public static ObservableCollection<testSession> GetAll()
            {
                return new ObservableCollection<testSession>(Zen4SyncModel.Entities.testSessions);
            }
            /// <summary>
            /// Returns all the Test Sessions, the ones finished the latest are returned first.
            /// </summary>
            /// <returns></returns>
            public static ObservableCollection<testSession> GetAllOrdered()
            {
                // Convert Test Session to List so that the Order can be made on EndDateTime which is
                // not an edmx property.
                var testSessions = Zen4SyncModel.Entities.testSessions.ToList();
                var ordered = testSessions.OrderByDescending(ts => ts.EndDateTime);
                return new ObservableCollection<testSession>(ordered);
            }
        }

        public static class TestSessionCategory
        {
            public static ObservableCollection<testSessionCategory> GetAll()
            {
                return new ObservableCollection<testSessionCategory>(Zen4SyncModel.Entities.testSessionCategories);
            }
        }

        public static class ClientSession
        {
            public static clientSession Create()
            {
                clientSession session = new clientSession();
                session.clientSession_id = Guid.NewGuid();
                session.clientSession_startDateTime = DateTime.UtcNow.AddHours(3);

                return session;
            }
        }

        public static class ActivityContext
        {
            public static activityContext Create()
            {
                activityContext context = new activityContext();
                context.activityContext_id = Guid.NewGuid();

                return context;
            }
        }

        public static class TestServer
        {
            public static ObservableCollection<testServer> GetAll()
            {
                return new ObservableCollection<testServer>(Zen4SyncModel.Entities.testServers);
            }
        }
    }
}
